package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.domain.LogSysRunning;
import com.sxhalo.fsms.domain.SysTask;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.quartz.QuartzManager;
import com.sxhalo.fsms.quartz.TaskService;
import com.sxhalo.fsms.service.ISysTaskService;
import com.sxhalo.fsms.util.ApiResult;
import com.sxhalo.fsms.util.CookieUtils;
import com.sxhalo.fsms.util.TokenUtil;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sxhalo.fsms.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;


@Controller
@RequestMapping("/api")
public class SysTaskController extends BaseController {
	@Autowired
	private ISysTaskService sysTaskService;
	@Autowired
	private TaskService taskService;
	@Autowired
	QuartzManager quartzManager;

	@ResponseBody
	@RequestMapping(value = "/task", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findTask(Integer typeId, Integer current, Integer pageSize, HttpServletRequest req) {

		return findList(current, pageSize, "", "update_time desc", sysTaskService);
	}

	@ResponseBody
	@SysControllerLog(operationName = "新增定时任务", modelName = "定时任务")
	@RequestMapping(value = "/task", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveTask(@RequestBody SysTask record, HttpServletRequest req) {
		String token = CookieUtils.getCookieValue(req, "token");
		if (token != null) {
			Integer userId = TokenUtil.getUserId(token);
			Timestamp nowtime = new Timestamp(System.currentTimeMillis());
			record.setJobStatus("0");
			record.setUpdateTime(nowtime);
			record.setCreateTime(nowtime);
			record.setCreateUser(userId.toString());
			record.setUpdateUser(userId.toString());
			sysTaskService.insertSelective(record);
		}
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "更新定时任务", modelName = "定时任务")
	@RequestMapping(value = "/task", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateTask(@RequestBody SysTask record, String method, HttpServletRequest req) {
		String token = CookieUtils.getCookieValue(req, "token");
		try {
			// 修改之前先关闭任务
			taskService.stop(record);
			if (token != null) {
				Integer userId = TokenUtil.getUserId(token);
				Timestamp nowtime = new Timestamp(System.currentTimeMillis());
				record.setUpdateTime(nowtime);
				record.setJobStatus("0");
				record.setUpdateUser(userId.toString());
				sysTaskService.updateByPrimaryKeySelective(record);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ApiResult.retOK(req);

	}

	@ResponseBody
	@SysControllerLog(operationName = "更新定时任务状态", modelName = "定时任务")
	@RequestMapping(value = "/task/updateState", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updatestate(Long id, Integer state, HttpServletRequest req) throws SchedulerException {
		// 1开启 0关闭
		taskService.changeStatus(id, state.toString());



		return ApiResult.retOK(req);
	}

}
